package com.qf.myshop.controller;

import com.alibaba.fastjson.JSON;
import com.qf.myshop.service.TypeService;
import com.qf.myshop.entity.ResultData;
import com.qf.myshop.entity.Type;
import com.qf.myshop.service.impl.TypeServiceImpl;
import com.qf.myshop.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.qf.myshop.entity.ResultData.createFailResult;
import static com.qf.myshop.entity.ResultData.createSuccessResult;


@WebServlet("/type.do")
public class TypeServlet extends BaseServlet {
    private TypeService typeService = new TypeServiceImpl();
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         List<Type> list = typeService.findAll();
         response.setContentType("application/json;charset=utf-8");
         ResultData data = null;
         if (list == null || list.size() == 0){
             data = createFailResult(Constants.PRODUCT_TYPE_NOT_FOUND, "没有产品类型数据");
         }else {
             data = createSuccessResult(list);
         }
         response.getWriter().write(JSON.toJSONString(data));
    }
}
